package com.hxjy.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.hxjy.constant.Constant;
import com.hxjy.dao.EmployeeDao;
import com.hxjy.entity.Employee;

/**
 * 员工业务实现类
 * @author 李彬
 */
@Repository("employeeDao")
public class EmployeeDaoImpl extends CommonDaoImpl implements EmployeeDao{
	private static Log log = LogFactory.getLog(EmployeeDaoImpl.class);
	@Override
	public Employee Login(String userName, String password) {
		final String sql =" select * from EMPLOYEE_TB where username=? and password=? ";
		List<Employee> list = this.getJdbcTemplate().query(sql,new Object[]{userName,password},new BeanPropertyRowMapper(Employee.class));
		if(list.size()==1){
			return (Employee) this.getJdbcTemplate().queryForObject(sql,new Object[]{userName,password},new BeanPropertyRowMapper(Employee.class));
		}
		return null;
	}
	@Override
	public Object[] getEmployeeList(Employee employee,int offset) {
		StringBuffer sql = new StringBuffer(" select et.*,dt.departmentName departmentName,ct.collieryName collieryName,com.companyName companyName,gt.name groupName,rt.roleName from employee_tb et " +
				"left join department_tb dt on et.departmentId=dt.id " +
				"left join colliery_tb ct on et.collieryId=ct.id " +
				"left join company_tb com on et.companyId=com.id " +
				"left join group_tb gt on et.groupId=gt.id " +
				"left join role_tb rt on rt.id=et.roleId where 1=1  ");
		StringBuffer countSql = new StringBuffer( " select count(*) from employee_tb et where 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if(employee!=null){
			if(employee.getRealName()!=null&&!employee.getRealName().equals("")){
				sql.append(" and et.realName like ? ");
				countSql.append(" and et.realName like ? ");
				params.add("%" + employee.getRealName() + "%");
			}
		}
		sql.append(" limit ?,? ");
		params.add(offset);
		params.add(offset+Constant.MAX_PAGE_SIZE);
		return this.customQuery(sql.toString(), countSql.toString(), params, Employee.class);
		
	}
	@Override
	public void saveEmployee(Employee employee) {
		this.save(employee);
	}
	@Override
	public void updateEmployee(Employee employee) {
		this.update(employee);
		
	}
	@Override
	public void deleteEmployee(Long id) {
		Employee employee = this.getEmployeeById(id);
		this.delete(employee);
	}
	@Override
	public Employee getEmployeeById(Long id) {
		final String sql = " select et.*,dt.departmentName departmentName,ct.collieryName collieryName,com.companyName companyName,gt.name groupName from employee_tb et " +
		"left join department_tb dt on et.departmentId=dt.id " +
		"left join colliery_tb ct on et.collieryId=ct.id " +
		"left join company_tb com on et.companyId=com.id " +
		"left join group_tb gt on et.groupId=gt.id where et.id=? ";
		return (Employee) this.getJdbcTemplate().queryForObject(sql, new Object[]{id},new BeanPropertyRowMapper(Employee.class));
	}
	@Override
	public List<Employee> getEmployeeList() {
		final String sql = " select * from employee_tb ";			
		return this.getJdbcTemplate().query(sql, new BeanPropertyRowMapper(Employee.class));
	}
}
